package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpBmlistMapper;
import com.ruoyi.system.domain.SpBmlist;
import com.ruoyi.system.service.ISpBmlistService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 报名Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
public class SpBmlistServiceImpl implements ISpBmlistService 
{
    @Autowired
    private SpBmlistMapper spBmlistMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询报名
     * 
     * @param bmId 报名主键
     * @return 报名
     */
    @Override
    public SpBmlist selectSpBmlistByBmId(Long bmId)
    {
        return spBmlistMapper.selectSpBmlistByBmId(bmId);
    }

    /**
     * 查询报名列表
     * 
     * @param spBmlist 报名
     * @return 报名
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpBmlist> selectSpBmlistList(SpBmlist spBmlist)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spBmlist.setDatascope(datascope);
        if(datascope==4){
            spBmlist.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spBmlist.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spBmlist.setCheckHuiyuanId(0l);
            }

        }
        return spBmlistMapper.selectSpBmlistList(spBmlist);
    }

    @Override
    public List<SpBmlist> selectSpBmlistListWeb(SpBmlist spBmlist)
    {
        return spBmlistMapper.selectSpBmlistList(spBmlist);
    }

    /**
     * 新增报名
     * 
     * @param spBmlist 报名
     * @return 结果
     */
    @Override
    public int insertSpBmlist(SpBmlist spBmlist)
    {
        return spBmlistMapper.insertSpBmlist(spBmlist);
    }

    /**
     * 修改报名
     * 
     * @param spBmlist 报名
     * @return 结果
     */
    @Override
    public int updateSpBmlist(SpBmlist spBmlist)
    {
        return spBmlistMapper.updateSpBmlist(spBmlist);
    }

    /**
     * 批量删除报名
     * 
     * @param bmIds 需要删除的报名主键
     * @return 结果
     */
    @Override
    public int deleteSpBmlistByBmIds(Long[] bmIds)
    {
        return spBmlistMapper.deleteSpBmlistByBmIds(bmIds);
    }

    /**
     * 删除报名信息
     * 
     * @param bmId 报名主键
     * @return 结果
     */
    @Override
    public int deleteSpBmlistByBmId(Long bmId)
    {
        return spBmlistMapper.deleteSpBmlistByBmId(bmId);
    }
}
